package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.common.utils.JwtUtil;
import com.java.second_goods.entity.User;
import com.java.second_goods.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 */
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "用户相关接口")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户列表", description = "获取用户列表")
    public AjaxResult list(User user) {
        startPage(1, 10);
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取用户详细信息", description = "根据用户ID获取用户详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户
     */
    @PostMapping
    @Operation(summary = "新增用户", description = "新增用户")
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @PutMapping
    @Operation(summary = "修改用户", description = "修改用户")
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除用户", description = "删除用户")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPassword")
    @Operation(summary = "重置密码", description = "重置用户密码")
    public AjaxResult resetPassword(@RequestBody User user) {
        return toAjax(userService.resetPassword(user));
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "用户注册")
    public AjaxResult register(@RequestBody Map<String, String> params) {
        String username = params.get("username");
        String phone = params.get("phone");
        String password = params.get("password");

        User user = userService.register(username, phone, password);
        if (user != null) {
            return success("注册成功", user);
        } else {
            return error("手机号已被注册");
        }
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录")
    public AjaxResult login(@RequestBody Map<String, String> params) {
        String phone = params.get("phone");
        String password = params.get("password");

        User user = userService.login(phone, password);
        if (user != null) {
            // 生成JWT令牌
            String token = jwtUtil.generateToken(user.getUsername(), user.getId(), user.getUserType());

            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("user", user);

            return success("登录成功", data);
        } else {
            return error("手机号或密码错误");
        }
    }

    /**
     * 增加用户积分
     */
    @PutMapping("/addPoints/{userId}/{points}")
    @Operation(summary = "增加用户积分", description = "增加用户积分")
    public AjaxResult addPoints(@PathVariable("userId") Integer userId,
                                @PathVariable("points") Integer points) {
        return toAjax(userService.addUserPoints(userId, points));
    }
}